#pragma once

#include <string>
#include "Packet.h"
#include "DataLink.h"
#include "FileList.h"

class IFileMgrUI
{
public:
    virtual ~IFileMgrUI() {}
    virtual void OnInsert(const char* strPath, bool bIsDir) = 0;
    virtual void OnDiskInfo(int iDiskId, uint32_t uTotal, uint32_t uFreex) = 0;
};

class FileMgrEngine:public IEngine
{
public:
    FileMgrEngine(IFileMgrUI* pMgr)
        : m_pFileMgr(pMgr)
        , m_Serial(Serial::AsynchronousOperate)
    {
        m_bConnected = false;
    }
    virtual ~FileMgrEngine() {}

public:
    IFileMgrUI* m_pFileMgr;

private:
    Serial      m_Serial;
    CFileList   m_FileList;
    bool        m_bConnected;
    UINT        m_uSendCnt;
    UINT        m_uFailCnt;

private:
    bool InsertFile(std::string strPath);
    bool InsertFolder(std::string strPath);
    bool Transmit(CFileList* flist, std::string strPath, std::string strTarget);

public:
    virtual void OnDiskInfo(int iDiskId, uint32_t uTotal, uint32_t uFreex);
    virtual void OnInsertFile(const char* strPath, bool bIsDir);

    bool LoadFile(std::string strPath);
    bool Transmit(const char* strPath, const char* strTarget);

    void SetEngine(IEngine *engine);
    bool Connect(const char* strPath);
    bool GetFiles(const char* strPath);
    bool FormatDisk(const char* name);
    bool DownLoad(const char* name);
    bool RemoveFile(const char* name);

    bool OpenSerial(const char* name, uint32_t bandrate = 115200);
    bool CloseSerial(); 

    void GetTranState(UINT& uSendCnt, UINT& uFailCnt) { uSendCnt = m_uSendCnt; uFailCnt = m_uFailCnt; }
};

